﻿var Application = function () {
    var self = this;
    this.CurrentPage = null;
    this.LastPage = null;
    this.IsOverlayOpened = 0;
    this.Pages = {};
    this.User = {};
    this.IsGoingBack = false;
    this.HasPageChangedFromMenu = false;

    var lastViablePageHash = null;

    this.Menu = {
        container: $("#menu"),
        instance: null
    };

    this.MessagesFactory = new MessagesFactory();

    this.initialize = function () {
        $(document).bind('pullDown', function () {
            if (self.CurrentPage && self.CurrentPage.instance && self.CurrentPage.instance.getPullDownData)
                self.CurrentPage.instance.getPullDownData();
        });

        $(document).bind('overlayOpened', function () {
            self.IsGoingBack = false;
            self.IsOverlayOpened++;
        });

        $(document).bind('overlayClosed', function () { self.IsOverlayOpened = Math.max(self.IsOverlayOpened - 1, 0); });

        $(".dialog")
            .on('onShow', function () { $(document).trigger('overlayOpened'); })
            .on('onHide', function () { $(document).trigger('overlayClosed'); });
    };

    this.disconnect = function () {
        self.showLoadingScreen();
    };

    this.changePage = function (page, ignoreCanLeave, hasPageChangedFromMenu) {
        if (!page.instance)
            return;

        self.HasPageChangedFromMenu = hasPageChangedFromMenu || false;

        if (!page.instance.canNavigate())
            return;

        if (self.CurrentPage && self.CurrentPage.instance && self.CurrentPage.instance.canLeave && !ignoreCanLeave) {
            self.CurrentPage.instance.canLeave(function (canLeave) {
                if (canLeave)
                    changePageApproved(page);
            });
        } else changePageApproved(page);
    };

    var changePageApproved = function (page) {
        if (app.CurrentPage && app.CurrentPage.instance && page && page.instance == app.CurrentPage.instance)
            return;

        if (page.instance.changePage) {
            if (page.instance.changePage()) {

                if (app.CurrentPage && app.CurrentPage.instance)
                    app.CurrentPage.instance.hideActions();

                var lastPageHash = self.CurrentPage ? self.CurrentPage.container.selector : self.Pages.Dashboard.container.selector;
                if (self.CurrentPage && self.CurrentPage.instance && !self.CurrentPage.instance.CanGoBack)
                    lastPageHash = page.instance.LastPageHash;
                if (!lastPageHash)
                    lastPageHash = lastViablePageHash;
                if (!lastPageHash)
                    lastPageHash = self.Pages.Dashboard.container.selector;
                lastViablePageHash = lastPageHash;

                var isGoingBack = self.CurrentPage && self.CurrentPage.instance && page && self.CurrentPage.instance.LastPageHash == page.container.selector;
                self.LastPage = self.CurrentPage;
                self.CurrentPage = page;

                if (!isGoingBack)
                    self.CurrentPage.instance.LastPageHash = lastPageHash;
                self.CurrentPage.container.trigger('onPageChanged');
                location.hash = self.CurrentPage.container.selector;
            } else self.changePage(self.CurrentPage);
        }
    };

    this.showLoadingScreen = function () { };

    this.hideLoadingScreen = function () { };

    this.translatePage = function (page) {
        self.translateElement(page.container);
    };

    this.translateElement = function (el) {
        el.find('.translate').each(function () {
            translateAttribute($(this), 'value');
            translateAttribute($(this), 'title');
            translateAttribute($(this), 'placeholder');
            if ($(this).is('img'))
                translateAttribute($(this), 'alt');
            translateAttribute($(this), 'content');
        });
    };

    var translateAttribute = function (el, attrName) {
        var attrValue = el.attr(Constants.TranslatePrefix + '-' + attrName) || "";
        var prefix = el.attr('prefix') || "";
        var sufix = el.attr('sufix') || "";

        if (attrValue) {
            var text = translate(attrValue);
            text = prefix + text + sufix;
            if (attrName != 'content')
                el.attr(attrName, text);
            else el.html(text);
        }
    };

    this.getPageByHash = function (hash) {
        for (var p in self.Pages)
            if (self.Pages[p].container.selector == hash)
                return self.Pages[p];

        return null;
    };

    this.goBack = function () {
        self.IsGoingBack = true;

        history.back();
    };
};